package com.innoking.docsys.user.service.impl;

import com.innoking.docsys.search.module.CmsJudVolumeExampleVO;
import com.innoking.docsys.search.module.CmsSinItemExampleVO;
import com.innoking.docsys.search.module.SolrFieldVO;
import com.innoking.docsys.user.service.PDFFileConversionService;
import com.innoking.docsys.user.service.SolrAllTextSearchService;
import com.innoking.docsys.user.util.ApplicationContextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;

/**
 * @ClassName SynchronizeSolrAndMysqlJob
 * @Description 同步Oracle中单件表，卷表，卷内件表至solr索引库
 * @Author wr
 * @Date 2019/8/5 000515:10
 */
public class SynchronizeSolrAndMysqlJob {
    private static Logger logger = LoggerFactory.getLogger(SynchronizeSolrAndMysqlJob.class);

    /**
     * 定时同步Oracle和solr索引库
     * 目前同步单件库，卷库，卷内件库
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void synchronizeSolrAndMysqlQuartz() {
        logger.info("进入定时同步Oracle和solr索引库方法*****************************************");
        ArrayList<SolrFieldVO> listSolrVO = new ArrayList<>();
        int id = 0;
        //1.查询卷表
        CmsJudVolumeExampleVO cmsJudVolumeExample = new CmsJudVolumeExampleVO();
        CmsJudVolumeExampleVO.Criteria criteria = cmsJudVolumeExample.createCriteria();
        //归档步骤（0：著录，1：待归档，2：已归档,3:提交驳回,4:归档驳回.5：归档失败）
        criteria.andVolArchiveStatusEqualTo(2);
        //升序降序
        cmsJudVolumeExample.setOrderByClause("\"VOL_ID\" ASC");
        //TODO:根据归档步骤查询
//        List<CmsJudVolumeVO> cmsJudVolumes = volAndItemService.selectAllByQueryParms(cmsJudVolumeExample);

        PDFFileConversionService pdfFileConversionService = (PDFFileConversionService) ApplicationContextUtil.getBean(PDFFileConversionService.class);
        /*if(cmsJudVolumes.size()!=0){
            for (CmsJudVolumeVO cmsJudVolume : cmsJudVolumes) {
                SolrFieldVO solrFieldVO = new SolrFieldVO();
                id+=1;
                solrFieldVO.setId(String.valueOf(id));
                //卷
                solrFieldVO.setTypeTable("vol");
                solrFieldVO.setArcId(cmsJudVolume.getVolId().toString());
                solrFieldVO.setTitle(cmsJudVolume.getVolTitle());
                solrFieldVO.setItemOrVolNum(cmsJudVolume.getVolCaseNum());
                solrFieldVO.setVolNo(cmsJudVolume.getVolNo());
                solrFieldVO.setAuditLeader(cmsJudVolume.getVolAuditLeader());
                *//*卷无文号
                solrFieldVO.setTextNum();*//*
                solrFieldVO.setArcClass(cmsJudVolume.getVolClass());
                solrFieldVO.setStorageTime(cmsJudVolume.getVolStorageTime());
                solrFieldVO.setArchiveYear(cmsJudVolume.getVolArchiveYear());
                solrFieldVO.setArchiveUnit(cmsJudVolume.getVolArchiveUnit());
                solrFieldVO.setSecretLevel(cmsJudVolume.getVolSecret());
                solrFieldVO.setVolTotal(cmsJudVolume.getVolTotal());
                solrFieldVO.setArchivePerson(cmsJudVolume.getVolArchivePerson());
               *//*卷无发文日期
               solrFieldVO.setPostDate();*//*
                solrFieldVO.setItemStatus(String.valueOf(cmsJudVolume.getVolStatus()));
                solrFieldVO.setWhetherPublic(String.valueOf(cmsJudVolume.getVolPublicStatus()));
                solrFieldVO.setCarrierType(cmsJudVolume.getVolCarrier());
                *//*卷层次无原文
                solrFieldVO.setPdfUrl();
                solrFieldVO.setPdfContent();*//*
                listSolrVO.add(solrFieldVO);
                //2.查询卷内件
                Long volId = cmsJudVolume.getVolId();
                //根据主键ID查询
//                List<CmsJudItemVO> cmsJudItems = volAndItemService.selectAllByVolId(volId, 1);
                for (CmsJudItemVO cmsJudItem : cmsJudItems) {
                    SolrFieldVO volItemVO = new SolrFieldVO();
                    id+=1;
                    volItemVO.setId(String.valueOf(id));
                    //卷内件
                    volItemVO.setTypeTable("volItem");
                    volItemVO.setArcId(String.valueOf(cmsJudItem.getItemId()));
                    volItemVO.setTitle(cmsJudItem.getItemTitle());
                    volItemVO.setItemOrVolNum(cmsJudItem.getItemStr1());
                   *//*j卷内件无案卷号
                   volItemVO.setVolNo();
                   卷内件无审计组长
                    volItemVO.setAuditLeader();
                    *//*
                    volItemVO.setTextNum(cmsJudItem.getItemName());
                    *//*卷内件无案卷类型
                    volItemVO.setArcClass();
                    volItemVO.setStorageTime();
                    volItemVO.setArchiveYear();
                    volItemVO.setArchiveUnit();
                    *//*
                    volItemVO.setSecretLevel(cmsJudItem.getItemSecret());
                    *//*volItemVO.setVolTotal(cmsJudVolume.getVolTotal());*//*
                    volItemVO.setArchivePerson(cmsJudItem.getItemRespons());
                    volItemVO.setPostDate(cmsJudItem.getItemCreateTime());
                    volItemVO.setItemStatus(String.valueOf(cmsJudItem.getItemStatus()));
                    volItemVO.setWhetherPublic(cmsJudItem.getItemStr5());
                    *//*volItemVO.setCarrierType();*//*
                    String itemStr2 = cmsJudItem.getItemStr2();
                    if(StringUtils.isNotBlank(itemStr2)){
                        volItemVO.setPdfUrl(itemStr2);
                        //调用原文获取接口
                        String pdfContent = pdfFileConversionService.PDFFileConversions(itemStr2);
                        logger.info("PDF中的文段********:"+pdfContent);
                        pdfContent = MathUtils.replaceBlank(pdfContent);
                        volItemVO.setPdfContent(pdfContent);
                    }
                    listSolrVO.add(volItemVO);
                }
            }
        }*/
        //3.查询单件
        CmsSinItemExampleVO cmsSinItemExample = new CmsSinItemExampleVO();
//        cmsSinItemExample.setOrderByClause("\"ITEM_ID\" ASC");
        CmsSinItemExampleVO.Criteria criteria1 = cmsSinItemExample.createCriteria();
        //归档步骤（0：著录，1：已归档,2:归档驳回,3:归档失败）
        criteria1.andItemArcStatusEqualTo(1L);
        //TODO:根据归档步骤查询
//        List<CmsSinItemVO> cmsSinItems = cmsSinItemService.selectAllByQueryParms(cmsSinItemExample);
        /*if (cmsSinItems.size() != 0) {
            FileWriter fw = null;
            for (CmsSinItemVO cmsSinItem : cmsSinItems) {
                id += 1;
                SolrFieldVO itemVO = new SolrFieldVO();
                itemVO.setId(String.valueOf(id));
                //单件
                itemVO.setTypeTable("sinItem");
                itemVO.setArcId(String.valueOf(cmsSinItem.getItemId()));
                itemVO.setTitle(cmsSinItem.getItemTitle());
                itemVO.setItemOrVolNum(cmsSinItem.getItemStr1());
                   *//*j卷内件无案卷号
                   itemVO.setVolNo();
                   卷内件无审计组长
                    itemVO.setAuditLeader();
                    *//*
                itemVO.setTextNum(cmsSinItem.getItemName());
                itemVO.setArcClass(cmsSinItem.getItemExp1());
                itemVO.setStorageTime(cmsSinItem.getItemNum());
                itemVO.setArchiveYear(cmsSinItem.getItemExp3());
                itemVO.setArchiveUnit(cmsSinItem.getItemExp2());
                itemVO.setSecretLevel(cmsSinItem.getItemSecret());
                itemVO.setVolTotal(cmsJudVolume.getVolTotal());
                itemVO.setArchivePerson(cmsSinItem.getItemRespons());
                itemVO.setPostDate(cmsSinItem.getItemCreateTime());
                itemVO.setItemStatus(String.valueOf(cmsSinItem.getItemStatus()));
                itemVO.setWhetherPublic(String.valueOf(cmsSinItem.getItemPublicStatus()));
                itemVO.setCarrierType(cmsSinItem.getItemCarrier());
                String itemStr2 = cmsSinItem.getItemStr2();
                if (StringUtils.isNotBlank(itemStr2)) {
                    itemVO.setPdfUrl(itemStr2);
                    //调用原文获取接口
                    String pdfContent = pdfFileConversionService.PDFFileConversions(itemStr2);
                    logger.info("PDF中的文段********:" + pdfContent);
                    pdfContent = MathUtils.replaceBlank(pdfContent);

                    try {
                        //首次将文本写入文件中，后续往里面追加内容
                        File f = new File("E:\\dd.txt");
                        fw = new FileWriter(f, true);
                        PrintWriter pw = new PrintWriter(fw);
                        pw.println(pdfContent);
                        pw.flush();
                        fw.flush();
                        pw.close();
                        fw.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    itemVO.setPdfContent(pdfContent);
                }
                listSolrVO.add(itemVO);
            }
        }*/
        if (listSolrVO.size() != 0) {
            //获取solr操作service
            SolrAllTextSearchService solrAllTextSearchService = (SolrAllTextSearchService) ApplicationContextUtil.getBean(SolrAllTextSearchService.class);
            solrAllTextSearchService.saveSolr(listSolrVO);
        }
    }
}
